package com.itbaizhan.shopping_admin_service.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_admin_service.mapper.AdminMapper;
import com.itbaizhan.shopping_common.pojo.Admin;
import com.itbaizhan.shopping_common.service.AdminService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: shopping
 * @ClassName AdminServiceImpl
 * @description:
 * @author: jing
 * @create: 2025-06-15 12:55
 **/

@DubboService
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public void add(Admin admin) {
        int insert = adminMapper.insert(admin);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.updateById(admin);
    }

    @Override
    public void delete(Long id) {
        //删除用户的所有用户
        adminMapper.deleteAdminAllRole(id);

        //删除用户
        adminMapper.deleteById(id);
    }

    @Override
    public Admin findById(Long id) {
        Admin admin = adminMapper.findById(id);
        return admin;
    }

    @Override
    public Page<Admin> search(int page, int size) {
        return adminMapper.selectPage(new Page<>(page, size), null);
    }

    @Override
    public void updateRoleToAdmin(Long aid, Long[] rids) {
        //删除改管理员的角色
        adminMapper.deleteAdminAllRole(aid);

        //添加该管理员的角色
        for (Long rid : rids) {
            adminMapper.addRoleToAdmin(aid,rid);
        }
    }
}
